#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

int main() {

    // Создать игровое поле для игроков 
    char array1[10][10];
    char array2[10][10];
    // Заполнить поля компьютера и человека
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            array1[i][j] = ' '; 
            array2[i][j] = ' '; 
        }
    } 
    
    int isEmpty = 44;
    
    array1[0][0] = 'o';
    array1[0][3] = 'o'; 
    array1[0][4] = 'o'; 
    array1[0][6] = 'o';
    array1[0][7] = 'o';
    array1[0][9] = 'o';
    array1[2][9] = 'o';
    array1[3][9] = 'o';
    array1[4][9] = 'o';
    array1[6][9] = 'o'; 
    array1[7][9] = 'o'; 
    array1[9][9] = 'o'; 
    array1[9][7] = 'o'; 
    array1[9][6] = 'o'; 
    array1[9][5] = 'o'; 
    array1[9][0] = 'o'; 
    array1[6][0] = 'o'; 
    array1[5][0] = 'o'; 
    array1[4][0] = 'o';
    array1[3][0] = 'o';

    array2[0][0] = 'o';
    array2[0][3] = 'o'; 
    array2[0][4] = 'o'; 
    array2[0][6] = 'o';
    array2[0][7] = 'o';
    array2[0][9] = 'o';
    array2[2][9] = 'o';
    array2[3][9] = 'o';
    array2[4][9] = 'o';
    array2[6][9] = 'o'; 
    array2[7][9] = 'o'; 
    array2[9][9] = 'o'; 
    array2[9][7] = 'o'; 
    array2[9][6] = 'o'; 
    array2[9][5] = 'o'; 
    array2[9][0] = 'o'; 
    array2[6][0] = 'o'; 
    array2[5][0] = 'o'; 
    array2[4][0] = 'o';
    array2[3][0] = 'o';
    
    int cellComp = 20;
    int cellHum = 20;
    
    printf("Do you want to load the previous game('y' or 'n')?\n");
    char answer[1];
    scanf("%s", answer);
    if (answer[0] == 'y') {
        FILE *file;
        file = fopen("/home/school/adelshin-dmitry-prog-school/sea-war/field.txt","rb");
        if (file != NULL) {
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    fread(&array1[i][j], 1, sizeof(char), file);           
                }
            }
            for (int i = 0; i < 10; i++) {
                for(int j = 0; j < 10; j++) {
                    fread(&array2[i][j], 1, sizeof(char), file); 
                }
            }
            fclose(file);
        }
    } 
    
    // Цикл пока можно играть
    while ((cellComp > 0) && (cellHum > 0)) {
        // Ход компьютера, найmи клетку для выстрела и выстрелить
        int i = 0, j = 0;
        if ((isEmpty <= 44) || (cellComp <= 20) || (cellHum <= 20)) {
            do {
                srand(time(NULL));
                i = rand() % 10;
                j = rand() % 10;
            } while ((array2[i][j] == '-') || (array2[i][j] == 'x'));
        }
         
        int up = array2[i + 1][j];
        int down = array2[i - 1][j];
        int left = array2[i][j + 1];
        int right = array2[i][j - 1];
        char direction;
        char direction1[4];
        
        direction1[0] = up;
        direction1[1] = down;
        direction1[2] = left;
        direction1[3] = right;
        
        do {
            srand(time(NULL));
            direction = rand() % 4;
        } while ((direction1[direction] == '-') || (direction1[direction] == 'x'));  
        
        if (direction == 0) {
            direction = direction1[0];
        }
        if (direction == 1) {
            direction = direction1[1];
        }
        if (direction == 2) {
            direction = direction1[2];
        }
        if (direction == 3) {
            direction = direction1[3];
        }
             
        if (array2[i][j] == 'o') {
            array2[i][j] = 'x';
            cellHum--;    
            if (direction == up) {
                if (array2[i + 1][j] == 'o') {
                    array2[i + 1][j] = 'x';
                    cellHum--;
                    if (array2[i + 2][j] == 'o') {
                        array2[i + 2][j] = 'x';
                        cellHum--;
                        if (array2[i + 3][j] == 'o') {
                            array2[i + 3][j] = 'x';
                            cellHum--;
                        } else {
                            array2[i + 3][j] = '-';
                            isEmpty--;
                        }
                    } else {
                        array2[i + 2][j] = '-';
                        isEmpty--;
                    }
                } else {
                    array2[i + 1][j] = '-';
                    isEmpty--;
                }
            } 
            if (direction == down) {
                if (array2[i - 1][j] == 'o') {
                    array2[i - 1][j] = 'x';
                    cellHum--;
                    if (array2[i - 2][j] == 'o') {
                        array2[i - 2][j] = 'x';
                        cellHum--;
                        if (array2[i - 3][j] == 'o') {
                            array2[i - 3][j] = 'x';
                            cellHum--;
                        } else {
                           array2[i - 3][j] = '-';
                            isEmpty--;
                        }
                    } else {
                        array2[i - 2][j] = '-';
                        isEmpty--;
                    }
                } else {
                    array2[i - 1][j] = '-';
                    isEmpty--;
                }
            }
            if (direction == left) {
                if (array2[i][j + 1] == 'o') {
                    array2[i][j + 1] = 'x';
                    cellHum--;
                    if (array2[i][j + 2] == 'o') {
                        array2[i][j + 2] = 'x';
                        cellHum--;
                        if (array2[i][j + 3] == 'o') {
                            array2[i][j + 3] = 'x';
                            cellHum--;
                        } else {
                            array2[i][j + 3] = '-';
                            isEmpty--;
                        }
                    } else {
                        array2[i][j + 2] = '-';
                        isEmpty--;
                    }
                } else {
                    array2[i][j + 1] = '-';
                    isEmpty--;
                }
            }  
            if (direction == right) {
                if (array2[i][j - 1] == 'o') {
                    array2[i][j - 1] = 'x';
                    cellHum--;
                    if (array2[i][j - 2] == 'o') {
                        array2[i][j - 2] = 'x';
                        cellHum--;
                        if (array2[i][j - 3] == 'o') {
                            array2[i][j - 3] = 'x';
                            cellHum--;
                        } else {
                            array2[i][j - 3] = '-';
                            isEmpty--;
                        }
                    } else {
                        array2[i][j - 2] = '-';
                        isEmpty--;
                    }
                } else {
                    array2[i][j - 1] = '-';
                    isEmpty--;
                }
            }  
        } else {
            array2[i][j] = '-';
            isEmpty--;
        }   
           
        int a = 0, b = 0;
        //Cпросить координаты выстрела
        printf("Enter coordinates or -1 if you want to exit: \n");
        scanf("%i", &a);
        scanf("%i", &b);
	    if ((a == -1) || (b == -1)) {
    	    printf("Do you want to save the game ('y' or 'n')\n");
			char answer[3];
            scanf("%s", answer); 
			if (answer[0] == 'y') {
				FILE *file;
        		file = fopen("/home/school/Projects/field.txt", "wb");
        		if (file != NULL) {
            		for (int i = 0; i < 10; i++) {
              			for (int j = 0; j < 10; j++) {
                			fwrite(&array1[i][j], 1, sizeof(char), file);           
              			}
           			 }
           			 for (int i = 0; i < 10; i++) {
              			for (int j = 0; j < 10; j++) {
               			    fwrite(&array2[i][j], 1, sizeof(char), file); 
             			}
            		 }
                     fclose(file);
                     break;
                 }	
			} else {
				break;
			}
		}
		if ((a == -1) || (b == -1)) {
		    break;
		}
        // Определить попадание или промах
        if (array1[a][b] == 'o') {
            array1[a][b] = 'x';
            cellComp--;
            
        }            
        if (array1[a][b] == ' ') {
            array1[a][b] = '-';
        }   
            
        printf("|");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                printf("%c", array2[i][j]);
                printf("|");
            }
            printf("\n");
            printf("|");
        }            
        printf("\n");
            
        printf("|");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (array1[i][j] == 'o') {
                    printf(" ");
                }
                if (array1[i][j] != 'o') {
                    printf("%c", array1[i][j]);
                }
                printf("|");
            }
            printf("\n");
            printf("|");
        }
        printf("\n");
    }
    // Если играть нельзя, то определить победителя
    if (cellComp == 0) {
        printf("Game Over! You win\n");
    }
    if (cellHum == 0) {
        printf("Game Over! You lose\n");
    }
    
    return 0;
}
